package com.ruoyi.framework.aspectj;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;



@Aspect
@Component
/**
 * 用于处理全局token
 */
public class CheckWechatTokenAspect extends BaseController {


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private ISysUserService sysUserService;


    @Pointcut("@annotation(com.ruoyi.common.annotation.CheckWechatToken)")
    public void checkWechatPointCut() {
    }


    @Around("checkWechatPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //获取头部的token
        String token = request.getHeader("token");
        if (StringUtils.isEmpty(token)) {
            throw new RuntimeException("非法请求");
        }
        //判断 库中是否存在
        SysUser user = sysUserService.selectUserByOpenId(token);
        if (null == user) {
            throw new RuntimeException("非法请求");
        }
        Object proceed = point.proceed();
        return proceed;
    }
}
